/*
 * Copyright (C) 2011 Johan Maasing johan at zoom.nu Licensed under the Apache
 * License, Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
 * or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package nu.zoom.bkkit.redlung;

import org.bukkit.ChatColor;
import org.bukkit.util.config.Configuration;

public class Config {
	private final static String SOURCE_NAME = Config.class.getName();
	private final Configuration config;

	public Config(Configuration config) {
		super();
		this.config = config;
	}

	int getRadiationOdds() {
		LogWrapper.entering(SOURCE_NAME, "getRadiationOdds", null);
		int radiationOdds = this.config.getInt("redlung.radiation.odds", 32);
		if (radiationOdds < 1) {
			radiationOdds = 1;
		}
		LogWrapper.fine("Radiation odds: " + radiationOdds);
		LogWrapper.exiting(SOURCE_NAME, "getRadiationOdds", null);
		return radiationOdds;
	}

	int getRadiationStrength() {
		LogWrapper.entering(SOURCE_NAME, "getRadiationStrength", null);
		int radiationStrength = this.config.getInt(
				"redlung.radiation.strength", 1);
		if (radiationStrength < 1) {
			radiationStrength = 1;
		}
		LogWrapper.fine("Radiation strength: " + radiationStrength);
		LogWrapper.exiting(SOURCE_NAME, "getRadiationStrength", null);
		return radiationStrength;
	}

	String getRadiatedMessage() {
		LogWrapper.entering(SOURCE_NAME, "getRadiatedMessage", null);
		final String message = this.config.getString(
				"redlung.radiation.message", ChatColor.GRAY
						+ "You feel a burning sensation");
		LogWrapper.exiting(SOURCE_NAME, "getRadiatedMessage", null);
		return message;
	}

	String getCuredMessage() {
		LogWrapper.entering(SOURCE_NAME, "getCuredMessage", null);
		final String message = this.config.getString("redlung.cured.message",
				ChatColor.DARK_GREEN + "You feel cured of redstone radiation");
		LogWrapper.exiting(SOURCE_NAME, "getCuredMessage", null);
		return message;
	}

	String getSickMessage(final int damage) {
		LogWrapper.entering(SOURCE_NAME, "getSickMessage", damage);
		final int messageNumber = (damage < RadiationAccumulator.MAX_ACCUMULATED_RADIATION / 2) ? 0
				: 1;
		final String message = this.config.getString("redlung.radiation.sick"
				+ messageNumber, (messageNumber == 0) ? ChatColor.DARK_RED
				+ "Your hair falls out and your stomach ache" : ChatColor.RED
				+ "You feel very ill and vomit blood");
		LogWrapper.exiting(SOURCE_NAME, "getSickMessage", damage);
		return message;
	}

	int getLogLevel() {
		return this.config.getInt("redlung.dev.loglevel", 0);
	}

	private boolean saveConfig() {
		return this.config.save();
	}

	public void disable() {
		LogWrapper.entering(SOURCE_NAME, "disable", null);
		saveConfig();
		LogWrapper.exiting(SOURCE_NAME, "disable", null);
	}

	public void enable() {
		// Used to load the defaults and save the file for creating it
		// the first time.
		getRadiatedMessage();
		getRadiationOdds();
		getRadiationStrength();
		getSickMessage(1);
		getSickMessage(RadiationAccumulator.MAX_ACCUMULATED_RADIATION);
		getCuredMessage();
		getLogLevel();
		saveConfig();
	}
}
